package com.kamistoat.meimeistore.product.service.impl;

import com.kamistoat.meimeistore.product.dao.AttrDao;
import com.kamistoat.meimeistore.product.entity.AttrEntity;
import com.kamistoat.meimeistore.product.vo.spuSave.BaseAttrs;
import com.kamistoat.meimeistore.product.dao.ProductAttrValueDao;
import com.kamistoat.meimeistore.product.entity.ProductAttrValueEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kamistoat.common.utils.PageUtils;
import com.kamistoat.common.utils.Query;

import com.kamistoat.meimeistore.product.service.ProductAttrValueService;
import org.springframework.transaction.annotation.Transactional;


@Service("productAttrValueService")
public class ProductAttrValueServiceImpl extends ServiceImpl<ProductAttrValueDao, ProductAttrValueEntity> implements ProductAttrValueService {

    @Autowired
    AttrDao attrDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<ProductAttrValueEntity> page = this.page(
                new Query<ProductAttrValueEntity>().getPage(params),
                new QueryWrapper<ProductAttrValueEntity>()
        );

        return new PageUtils(page);
    }

    /**
     * 并非服务层接口方法，只是在saveSPUInfo()中调用
     *
     * @param id            SPU_Id
     * @param baseAttrsList 基础属性列表
     */
    @Override
    public void saveSpuAttr(Long id, List<BaseAttrs> baseAttrsList) {
        if (baseAttrsList == null || baseAttrsList.size() == 0) {
        } else {
            List<ProductAttrValueEntity> productAttrValueEntityList = baseAttrsList.stream().map((baseAttrs)->{
                ProductAttrValueEntity productAttrValueEntity = new ProductAttrValueEntity();
                productAttrValueEntity.setSpuId(id);
                productAttrValueEntity.setAttrId(baseAttrs.getAttrId());
                productAttrValueEntity.setAttrName(
                        attrDao.selectOne(new QueryWrapper<AttrEntity>().
                                eq("attr_id",baseAttrs.getAttrId())).getAttrName());
                productAttrValueEntity.setAttrValue(baseAttrs.getAttrValues());
                productAttrValueEntity.setQuickShow(baseAttrs.getShowDesc());
                return productAttrValueEntity;
            }).collect(Collectors.toList());
            this.saveBatch(productAttrValueEntityList);
        }
    }

    /**
     * 自己的方法，返回对应spuId下的所有基础属性的值
     *
     * @param spuId spuId
     * @return List
     */
    @Override
    public List<ProductAttrValueEntity> listForSpu(Long spuId) {
        List<ProductAttrValueEntity> productAttrValueEntityList =
                baseMapper.selectList(new QueryWrapper<ProductAttrValueEntity>().eq("spu_id",spuId));
        return productAttrValueEntityList;
    }

    /**
     * 自己的方法，修改SpuId的基础属性值
     *
     * @param spuId                      spuId
     * @param productAttrValueEntityList 基础属性列表
     */
    @Override
    @Transactional
    public void updateBySpuId(Long spuId, List<ProductAttrValueEntity> productAttrValueEntityList) {
        // 先删除，因为存在把某些属性的值全部删除了，这个时候前端返回的json是不会携带这个字段的
        baseMapper.delete(new QueryWrapper<ProductAttrValueEntity>().eq("spu_id",spuId));
        List<ProductAttrValueEntity> pList = productAttrValueEntityList.stream().map(entity->{
            entity.setSpuId(spuId);
            return entity;
        }).collect(Collectors.toList());
        this.saveBatch(pList);
    }

}